package com.example.plugin.hook.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import android.content.Context;

import com.example.plugin.hook.HookHandler;
import com.example.plugin.hook.HookedMethodHandler;

public abstract class ProxyHook implements InvocationHandler {

	public Context mContext;
	public HookHandler mHookHandles;

	public Object mOldObj;

	public ProxyHook(Context context) {
		mContext = context;
		mHookHandles = createHookHandler();
	}

	public void setOldObj(Object oldObj) {
		this.mOldObj = oldObj;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		// TODO Auto-generated method stub
		HookedMethodHandler hookedMethodHandler = mHookHandles
				.getHookedMethodHandler(method);
		
		if (hookedMethodHandler != null) {
			return hookedMethodHandler.doHook(mOldObj, method, args);
		}
		
		return method.invoke(mOldObj, args);
	}
	
	public abstract HookHandler createHookHandler();

	public abstract void onInstall() throws Throwable;
}
